#!/usr/bin/python
# encoding: utf-8

import lish, commands, graphics
from const import *

def new_lish_data (mainform, path):
  res = lish.Lish(mainform, path, DefaultSkin())
  res.updateSkin()
  return res

def call_cmd (lish, index, selected, cmd):
  print index, selected
  try:
    if cmd == u'ls' or cmd.startswith(u'ls '):
      commands.listfiles(lish, cmd);
    elif cmd.startswith(u'cd '):
      commands.changedir(lish, cmd);
    elif cmd == u'quit':
      commands.quit(lish, cmd)
    else:
      return u'unknown command: ' + cmd
    return u''
  except Exception, e:
    return unicode(e)

def key_event (lish, index, selected, key, meta):
  if key == 186: # :
    lish.setCommandMode()
  elif key == 38: # up
    lish.up(1)
  elif key == 40: # down
    lish.down(1)
  elif key == 32: # space
    lish.toggleSelection(index)
    lish.down(1)
  elif key == 65 and meta == mkCTRL: # ctrl-A
    lish.selectAll()
  elif key == 65 and meta == (mkCTRL | mkSHIFT): # ctrl-shift-A
    lish.unselectAll()
  elif key == 93: # app key
    #lish.contextMenu()
    pass
  elif key == 13: # enter
    commands.run_default(lish, index)
  elif key == 8: # backspace
    commands.changedir(lish, 'cd ..')
  return u''

def mouse_event (lish, index, selected, type, mouse, meta):
  print type, mouse, meta
  if type == meDRAGSTART:
    lish.dragStart()
  elif type == meDRAGOVER:
    lish.setDragDropEffect(deMOVE)
  elif type == meDRAGDROP:
    lish.dragDropContextMenu()
  return u''

def contextmenu (lish, index, selected):
  lish.contextMenu()
  return u''

class DefaultSkin (lish.Skin):
  def __init__ (self):
    ftBlack = graphics.Font(u'ＭＳ Ｐゴシック', 9).setCharset(csSHIFTJIS)
    #ftBlack = Font(u'Courier New', 9).setPitch(fpFixed)
    lish.Skin.__init__(self, clSilver, clSkyBlue, [0,12,12,0], [ftBlack])

  def showFile (self, lish, idx):
    f = lish.filelist.files[idx]
    return [self.textlet(1,5,-1,0,f),
            self.textlet(2,5,-1,0,'FILE')]

